package interesado;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import misc.Logica;
import misc.Login_GUI;







/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class Registrarse_GUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblCartel;
	private JLabel lblNombre;
	private JTextField txtTelefono;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JPasswordField txtPasswordConfirmacion;
	private JPasswordField txtPassword;
	private JTextField txtDNI;
	private JLabel lblDNI;
	private JTextField txtNombre;
	private JTextField txtEmail;
	private JTextField txtApellido;
	private JTextField txtLU;
	private JLabel lblPasswordConfirmacion;
	private JLabel lblPassword;
	private JLabel lblEmail;
	private JLabel lblTelefono;
	private JLabel lblLU;
	private JLabel lblApellido;
	private Login_GUI padre;
	private Logica logica;

	

	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public Registrarse_GUI(Login_GUI p, Logica l) {
		super();
		padre = p;
		logica = l;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setLocationRelativeTo(padre);
			setTitle("Registrarse");
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lblCartel = new JLabel();
				getContentPane().add(lblCartel);
				lblCartel.setText("Registrarse");
				lblCartel.setBounds(47, 31, 129, 21);
				lblCartel.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre:");
				lblNombre.setBounds(47, 79, 89, 16);
				lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblApellido = new JLabel();
				getContentPane().add(lblApellido);
				lblApellido.setText("Apellido:");
				lblApellido.setBounds(74, 107, 62, 16);
				lblApellido.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblDNI = new JLabel();
				getContentPane().add(lblDNI);
				lblDNI.setText("DNI:");
				lblDNI.setBounds(89, 135, 47, 16);
				lblDNI.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblLU = new JLabel();
				getContentPane().add(lblLU);
				lblLU.setText("LU:");
				lblLU.setBounds(89, 163, 47, 16);
				lblLU.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblTelefono = new JLabel();
				getContentPane().add(lblTelefono);
				lblTelefono.setText("Teléfono:");
				lblTelefono.setBounds(68, 191, 68, 16);
				lblTelefono.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblEmail = new JLabel();
				getContentPane().add(lblEmail);
				lblEmail.setText("E-mail:");
				lblEmail.setBounds(68, 219, 68, 16);
				lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblPassword = new JLabel();
				getContentPane().add(lblPassword);
				lblPassword.setText("Password:");
				lblPassword.setBounds(68, 247, 68, 16);
				lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblPasswordConfirmacion = new JLabel();
				getContentPane().add(lblPasswordConfirmacion);
				lblPasswordConfirmacion.setText("Confirme Password:");
				lblPasswordConfirmacion.setBounds(14, 282, 122, 16);
				lblPasswordConfirmacion.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(148, 76, 103, 23);
			}
			{
				txtLU = new JTextField();
				getContentPane().add(txtLU);
				txtLU.setBounds(148, 160, 103, 23);
			}
			{
				txtApellido = new JTextField();
				getContentPane().add(txtApellido);
				txtApellido.setBounds(148, 105, 103, 23);
			}
			{
				txtEmail = new JTextField();
				getContentPane().add(txtEmail);
				txtEmail.setBounds(148, 216, 103, 23);
			}
			{
				txtDNI = new JTextField();
				getContentPane().add(txtDNI);
				txtDNI.setBounds(148, 132, 103, 23);
			}
			{
				txtTelefono = new JTextField();
				getContentPane().add(txtTelefono);
				txtTelefono.setBounds(148, 188, 103, 23);
			}
			{
				txtPassword = new JPasswordField();
				getContentPane().add(txtPassword);
				txtPassword.setBounds(148, 250, 103, 23);
			}
			{
				txtPasswordConfirmacion = new JPasswordField();
				getContentPane().add(txtPasswordConfirmacion);
				txtPasswordConfirmacion.setBounds(148, 279, 103, 23);
				txtPasswordConfirmacion.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						txtPasswordConfirmacionKeyReleased(evt);
					}
				});
				txtPassword.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						txtPasswordKeyReleased(evt);
					}
				});
			
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(176, 323, 89, 23);
				
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancelar();
					}
				});
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(47, 323, 89, 23);
				btnAceptar.setEnabled(false);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						aceptar();
					}
				}	);
			}
			pack();
			this.setSize(330, 400);
			this.setVisible(true);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	private void crearCuenta(String nombre, String apellido, String telefono,String email,String password,int dni,int lu){
		logica.crearCuenta(nombre, apellido, telefono, email, password,dni,lu);
	}
	
	private void aceptar()
	{
		//Validamos
		String nombre=txtNombre.getText();
		String apellido=txtApellido.getText();
		String email=txtEmail.getText();
		String password=txtPassword.getText();
		String confirmacionPassword=txtPasswordConfirmacion.getText();
		String telefono=txtTelefono.getText();
		String dni=txtDNI.getText();
		String lu=txtLU.getText();
		if(nombre.equals("")||apellido.equals("")||email.equals("")||password.equals("")||confirmacionPassword.equals("")||telefono.equals("")||dni.equals("")||lu.equals(""))
			JOptionPane.showMessageDialog(null,"Debe completar todos los campos","Error",JOptionPane.ERROR_MESSAGE);
		else if(!txtPassword.getText().equals(txtPasswordConfirmacion.getText()))
			JOptionPane.showMessageDialog(null,"El password y su confirmaci�n son diferentes","Error",JOptionPane.ERROR_MESSAGE);
		else
			try
		    {
			int dni_aux = Integer.parseInt(dni);
			int lu_aux = Integer.parseInt(lu);
			int respuesta = JOptionPane.showConfirmDialog(null, "Confirma la operacion", "Registrar Interesado",
                    JOptionPane.YES_NO_OPTION);
			
			 //Confirmacion afirmativa, sino volvemos a la pantalla
			 if(respuesta==JOptionPane.YES_OPTION){
			crearCuenta(nombre,apellido,telefono,email,password,dni_aux,lu_aux);
			
			this.setVisible(false);
			padre.setVisible(true);
			this.dispose();}
					
		    }
		    catch (NumberFormatException e)
		    {
		    	JOptionPane.showMessageDialog(null,"El DNI y el LU deben ser numéricos.","Error",JOptionPane.ERROR_MESSAGE);
		    }
	
	
	}
	private boolean isPasswordCorrect(char[] j1,char[] j2) {
		boolean valor = true;
		int puntero = 0;
		if (j1.length != j2.length){
		valor = false;
		}
		else{
		while((valor)&&(puntero < j1.length)){
		if (j1[puntero] != j2[puntero]){
		valor = false;
		}
		puntero++;
		}
		}
		return valor;
		}
	private void comparaPass() {
		if(!isPasswordCorrect(txtPassword.getPassword(),txtPasswordConfirmacion.getPassword()))
		{
			txtPasswordConfirmacion.setBackground(Color.red);
		btnAceptar.setEnabled(false );
	}
			else
				{
				btnAceptar.setEnabled(true);
				
				txtPasswordConfirmacion.setBackground(Color.green);
				}
	}
	private void txtPasswordConfirmacionKeyReleased(KeyEvent evt) {
		comparaPass();
		
				
		
			
	}

	private void txtPasswordKeyReleased(KeyEvent evt) {
		comparaPass();
	}

	private void cancelar()
	{
		this.setVisible(false);
		padre.setVisible(true);
		this.dispose();
	}
}
